package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.KeywordPageQueryDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {
    @Autowired
    WmNewsService wmNewsService;

    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(this.list());
    }

    @Override
    public ResponseResult del(Integer id) {
        if (id==null)
        {
            throw new CustomException(AppHttpCodeEnum.PARAMS_NOT_NULL);
        }
        List<WmNews> list = wmNewsService.list(Wrappers.<WmNews>lambdaQuery().eq(WmNews::getChannelId, id));
        if (!list.isEmpty())
        {
            throw new CustomException(AppHttpCodeEnum.CHANNEL_USED);
        }
        boolean b = this.removeById(id);
        return b?ResponseResult.okResult(AppHttpCodeEnum.SUCCESS):ResponseResult.errorResult(AppHttpCodeEnum.FAIL);
    }

    @Override
    public ResponseResult pageQuery(KeywordPageQueryDto dto) {
        dto.checkParam();
        LambdaQueryWrapper<WmChannel> lqw=new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(dto.getName()),WmChannel::getName,dto.getName());
        IPage page=new Page(dto.getPage(),dto.getSize());
        page = this.page(page, lqw);
        return ResponseResult.okResult(page);
    }

    @Override
    public ResponseResult add(WmChannel channel) {
        if (channel==null)
        {
            throw new CustomException(AppHttpCodeEnum.PARAMS_NOT_NULL);
        }
        List<WmChannel> list = this.list(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getName, channel.getName()));
        if (!list.isEmpty())
        {
            throw new CustomException(AppHttpCodeEnum.DATA_EXIST);
        }
        boolean b = this.save(channel);
        return b?ResponseResult.okResult(AppHttpCodeEnum.SUCCESS):ResponseResult.errorResult(AppHttpCodeEnum.FAIL);
    }

    @Override
    public ResponseResult update(WmChannel channel) {
        if (channel==null)
        {
            throw new CustomException(AppHttpCodeEnum.PARAMS_NOT_NULL);
        }
        boolean b = this.updateById(channel);
        return b?ResponseResult.okResult(AppHttpCodeEnum.SUCCESS):ResponseResult.errorResult(AppHttpCodeEnum.FAIL);
    }
}